package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.Setmeal;
import com.itheima.service.ReportService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.*;

@Service(interfaceClass = ReportService.class)
@Transactional
public class ReportServiceImpl implements ReportService {
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private SetmealDao setmealDao;
    @Autowired
    private OrderDao orderDao;

    @Override
    public Map<String, Object> getMemberReport() {
        Map<String, Object> map = new HashMap<>();
        List<String> mouths = new ArrayList<>();
        List<Integer> mouthDates = new ArrayList<>();

        LocalDate localDate = LocalDate.now();
        for (int i = 11; i >= 0; i--) {
            String mouth = localDate.minusMonths(i).toString().substring(0, 7);
            mouths.add(mouth);
            Integer mouthDate = memberDao.findMemberCountByMouth(mouth);
            mouthDates.add(mouthDate);
        }
        map.put("months", mouths);
        map.put("memberCount", mouthDates);

        return map;
    }

    @Override
    public Map<String, Object> getSetmealReport() {
        Map<String, Object> resultMap = new HashMap<>();
        List<String> setmealName = new ArrayList<>();
        List<Map<String, Object>> setmealCount = new ArrayList<>();
        // 从dao中查出的数据
        List<Map<String, Integer>> setmealReport = setmealDao.getSetmealReport();
        for (Map map : setmealReport) {
            setmealName.add((String) map.get("name"));
            setmealCount.add(map);
        }
//        setmealName.add("入职体检");
//        setmealName.add("入职体2");
//        setmealName.add("入职体3");

//        for (String s : setmealName) {
//            Map<String, Object> count = new HashMap<>();
//            count.put("name",s);
//            count.put("value",50);
//            setmealCount.add(count);
//        }
        resultMap.put("setmealNames", setmealName);
        resultMap.put("setmealCount", setmealCount);
        return resultMap;
    }

    @Override
    public Map<String, Object> getBusinessReportData() throws Exception {
        //获得当前日期
        String today = DateUtils.parseDate2String(DateUtils.getToday());
        //获得本周一的日期
        String thisWeekMonday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
        //获得本月第一天的日期
        String firstDay4ThisMonth = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());
        //今日新增会员数
        Integer todayNewMember = memberDao.findMemberCountByDate(today);
        //总会员数
        Integer totalMember = memberDao.findMemberTotalCount();
        //本周新增会员数
        Integer thisWeekNewMember = memberDao.findMemberCountAfterDate(thisWeekMonday);
        //本月新增
        Integer thisMonthNewMember = memberDao.findMemberCountAfterDate(firstDay4ThisMonth);
        //今日预约数
        Integer todayOrderNumber = orderDao.findOrderCountByDate(today);
        //本周预约数
        Integer thisWeekOrderNumber = orderDao.findOrderCountAfterDate(thisWeekMonday);
        //本月预约数
        Integer thisMonthOrderNumber = orderDao.findOrderCountAfterDate(firstDay4ThisMonth);
        //今日到诊数
        Integer todayVisitsNumber = orderDao.findVisitsCountByDate(today);
        //本周到诊数
        Integer thisWeekVisitsNumber = orderDao.findVisitsCountAfterDate(thisWeekMonday);
        //本月到诊数
        Integer thisMonthVisitsNumber = orderDao.findVisitsCountAfterDate(firstDay4ThisMonth);
        // 热门套餐
        List<Map> hotSetmeal = orderDao.findHotSetmeal2();
        Map<String,Object> result = new HashMap<>();
        result.put("reportDate",today);
        result.put("todayNewMember",todayNewMember);
        result.put("totalMember",totalMember);
        result.put("thisWeekNewMember",thisWeekNewMember);
        result.put("thisMonthNewMember",thisMonthNewMember);
        result.put("todayOrderNumber",todayOrderNumber);
        result.put("thisWeekOrderNumber",thisWeekOrderNumber);
        result.put("thisMonthOrderNumber",thisMonthOrderNumber);
        result.put("todayVisitsNumber",todayVisitsNumber);
        result.put("thisWeekVisitsNumber",thisWeekVisitsNumber);
        result.put("thisMonthVisitsNumber",thisMonthVisitsNumber);
        result.put("hotSetmeal",hotSetmeal);
        return result;
    }

    @Override
    public Map<Member, List<Setmeal>> exportMemberExcel(List<Member> list) {
        Map<Member, List<Setmeal>> result = new HashMap<>();
        // 遍历前台过来的用户
        for (Member member : list) {
            List<Setmeal> setmealList = new ArrayList<>();
            // 查询用户预约的套餐id
            List<Order> orderByMemberId = orderDao.findByMemberId(member.getId());
            for (Order order : orderByMemberId) {
                // 通过order获取套餐id,在通过套餐id查询详细的套餐
                Setmeal setmeal = setmealDao.setmealInfoById(order.getSetmealId());
                setmealList.add(setmeal);
            }
            result.put(member,setmealList);
        }
        return result;
    }
}
